package com.lfg.leetcode.leetcode0;

/**
 * You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
 * <p>
 * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
 * Output: 7 -> 0 -> 8
 */
public class No2AddTwoNumber {


    public static ListNode addTwoNumbers(ListNode number1, ListNode number2) {
        ListNode currentNode = null;
        ListNode ret = null;
        int total = 0;
        while (number1 != null || number2 != null) {
            if (null != number1) {
                total = total + number1.val;
                number1 = number1.next;
            }
            if (null != number2) {
                total = total + number2.val;
                number2 = number2.next;
            }
            ListNode node = new ListNode(total % 10);
            if (null != currentNode) {
                currentNode.next = node;
            } else {
                ret = node;
            }
            currentNode = node;
            total = total / 10;
        }
        if (total > 0) {
            currentNode.next = new ListNode(total);
        }

        return ret;
    }

    public static void main(String[] args) {
        ListNode number1 = new ListNode(9);
//        number1.next = new ListNode(4);
//        number1.next.next = new ListNode(3);
//        number1.next.next.next = new ListNode(1);


        ListNode number2 = new ListNode(1);
        number2.next = new ListNode(9);
        number2.next.next = new ListNode(9);
        number2.next.next.next = new ListNode(9);
        number2.next.next.next.next = new ListNode(9);
        number2.next.next.next.next.next = new ListNode(9);
        number2.next.next.next.next.next.next = new ListNode(9);
        number2.next.next.next.next.next.next.next = new ListNode(9);
        number2.next.next.next.next.next.next.next.next = new ListNode(9);
        number2.next.next.next.next.next.next.next.next.next = new ListNode(9);

        ListNode ret = addTwoNumbers(number1, number2);
        while (ret != null) {
            System.out.print(ret.val + " - ");
            ret = ret.next;
        }
    }
}
